package com.cuit.ocp.controller;

import com.alibaba.fastjson.JSONArray;
import com.cuit.ocp.bean.Staff;
import com.cuit.ocp.service.OcpStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@CrossOrigin(origins ="*",maxAge = 3600)
@RestController
@RequestMapping("staff")
public class OcpStaffController {

    @Autowired
    public OcpStaffService ocpStaffService;

    @PostMapping("findAll")
    public HashMap<Object,Object> findAll(){
        System.out.println("@@@");
        HashMap<Object,Object> result = new HashMap<>();
        List<Staff> staff_info = ocpStaffService.findAll();
        if(staff_info.size() > 0){
            result.put("new_staff",staff_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @PostMapping("findById")
    public HashMap<Object,Object> findStaffById(int staff_id){
        HashMap<Object,Object> result = new HashMap<>();
        List<Staff> staff_info = ocpStaffService.findById(staff_id);
        if(staff_info != null){
            result.put("new_staff",staff_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @PostMapping("findByName")
    public HashMap<Object,Object> findByName(String staff_name){
        HashMap<Object,Object> result = new HashMap<>();
        List<Staff> staff_info = ocpStaffService.findByName(staff_name);
        if(staff_info != null){
            result.put("new_staff",staff_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @PostMapping("addNewStaff")
    public HashMap<Object,Object> addNewStaff(Staff new_staff){
        HashMap<Object,Object> result = new HashMap<>();
        if(ocpStaffService.addNewStaff(new_staff) == true){
            result.put("new_staff",new_staff);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
            return result;
    }


    @PostMapping("update")
    public HashMap<Object,Object> update(Staff staff_info){
        HashMap<Object,Object> result = new HashMap<>();
        if(ocpStaffService.update(staff_info) == true){
            result.put("new_staff",staff_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @PostMapping("delete")
    public HashMap<Object,Object> deleteById(int staff_id){
        HashMap<Object,Object> result = new HashMap<>();
        if(ocpStaffService.deleteById(staff_id) == true)
            result.put("message","success");
        else
            result.put("message","failure");

        return result;
    }
    @PostMapping("deletes")
    public HashMap<Object,Object> deleteByIds(@RequestBody String staff_ids){
        HashMap<Object,Object> result = new HashMap<>();
        JSONArray json_staffIDs= JSONArray.parseArray(staff_ids);
        List<Integer> ids = new ArrayList<>();
        for(int i=0;i<json_staffIDs.size();i++)
            ids.add((Integer) json_staffIDs.get(i));
        if(ocpStaffService.deleteByIds(ids) == true)
            result.put("message","success");
        else
            result.put("message","failure");

        return result;
    }
}
